package com.tsinghuabigdata.edu.ddmath.module.login.view;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.MVPModel.RequestListener;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.activity.RoboForToolBarActivity;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.module.login.LoginModel;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.AlertManager;
import com.tsinghuabigdata.edu.ddmath.view.CustomOptionPicker;
import com.tsinghuabigdata.edu.ddmath.view.MyProgressDialog;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 个人信息补充界面
 * Created by Administrator on 2016/12/14.
 */

public class UserInfoSuplementActivity extends RoboForToolBarActivity implements View.OnClickListener {
    private static final String TAG = "UserInfoSuplementFragment";

    private Button btEnterdoudou;

    @ViewInject(R.id.et_reallyname)
    private EditText etReallyName;
    @ViewInject(R.id.et_school)
    private AutoCompleteTextView etSchool;
    @ViewInject(R.id.et_encrollyears)
    private TextView etEncrollyears;

    private MyProgressDialog progressDialog;
    private Context context;
    private CustomOptionPicker picker;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_userinfo_suplement;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        initView();
        initData();
    }

    private void initView() {
        setLeftTitle("取消", false);
        setTitle("个人信息");
        setBarTextcolor(R.color.white);
        setUserBarBackgroud(true);

        btEnterdoudou = (Button) findViewById(R.id.bt_enterdoudou);
        btEnterdoudou.setOnClickListener(this);
        etEncrollyears.setOnClickListener(this);

        progressDialog = new MyProgressDialog(context);

        initDatepicker();
    }

    private void initData() {
    }

    @Override
    public void onLeftClick() {
        super.onLeftClick();

        finish();
        //to usercenter fragment
    }


    private void initDatepicker() {
        List<String> years = new ArrayList<>();
        int currYear = Calendar.getInstance().get(Calendar.YEAR);
        String pre = "                                                                                                    ";
        String suff = "                                                                                                     ";
        for (int starty = 1990; starty <= currYear; starty++) {
            years.add(pre + starty + "年" + suff);
        }
        String[] strYears = (String[]) years.toArray(new String[years.size()]);
        picker = new CustomOptionPicker(this, strYears);
        picker.setOffset(2);
        if (years.size() > 0) {
            picker.setSelectedIndex(years.size() - 1);
        }
        picker.setTextSize(19);
        picker.setTitleText("输入入学年份");
        picker.setTopBackgroundColor(getResources().getColor(R.color.bar_tint));
        picker.setCancelTextColor(getResources().getColor(R.color.white));
        picker.setTitleTextColor(getResources().getColor(R.color.white));
        picker.setSubmitTextColor(getResources().getColor(R.color.white));

        picker.setTextColor(getResources().getColor(R.color.bartitle_text_color));
        picker.setLineColor(getResources().getColor(R.color.color_999999));
        picker.setOnOptionPickListener(new CustomOptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(String option) {
                etEncrollyears.setText(option.toString().trim());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_enterdoudou:
                enterDoudou();
                goUsercenter();
                break;
            case R.id.et_encrollyears:
                picker.show();
                break;
        }
    }

    private void enterDoudou() {
        //补充个人信息
        String reallyname = etReallyName.getText().toString().trim();
        String encrollyears = etEncrollyears.getText().toString().trim();
        if (TextUtils.isEmpty(reallyname) && TextUtils.isEmpty(encrollyears)) {
            //直接进入
            goUsercenter();
        } else {
            //将个人信息更新到服务器
            String accessToken = AccountUtils.getLoginUser().getAccessToken();
            String accountId = AccountUtils.getLoginUser().getAccountId();
            progressDialog.setMessage("更新信息中...");
            progressDialog.show();
            new LoginModel().updateExtraPersoninfo(accessToken, accountId, reallyname, encrollyears, new RequestListener() {
                @Override
                public void onSuccess(Object res) {
                    progressDialog.dismiss();
                    goUsercenter();
                }

                @Override
                public void onFail(HttpResponse response, Exception ex) {
                    AlertManager.showErrorInfo(context, ex);
                    progressDialog.dismiss();
                }
            });
        }
    }
}
